package com.zmcn.flowindex.fw.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public class TelBookToVCF {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// String[] ff={"=E6=88=91=E7=9A=84","=E7=88=B8","=E7=88=B8"};
		// String[] ff = { "=E6=88=91=E7=9A=84", "=E5=A6=88", "=E7=88=B8" };
		// for (String f : ff)
		// System.out.println(qpDecoding(f));
		// getSpitName("中国人民");

		String inPath = "D:/myBook.txt";
		String outPath = "D:/myBook.vcf";
		String c = FileOP.readFile(inPath, "gb2312");
		String[] lines = c.split("\r\n");
		StringBuffer sb = new StringBuffer();
		for (String s : lines) {
			s = s.trim();
			if (null != s && !"".equals(s)) {
				sb.append(getOneCard(s));
			}
		}
		FileOP.writeFile(outPath, sb.toString(), "gb2312");
		System.out.println(sb);
	}

	/**
	 * 解析一条记录
	 * 
	 * @param line
	 * @return
	 */
	public static String getOneCard(String line) {
		String[] s = line.split("\\|");
		String name = s[0];
		String phone = s[1];
		StringBuffer sb = new StringBuffer("BEGIN:VCARD\r\n");
		sb.append("VERSION:2.1\r\n");
		if (name.startsWith("en-")) {
			name = name.substring(3);
			sb.append("N:;" + name + ";;;\r\n");
			sb.append("FN:").append(name).append("\r\n");
		} else {
			sb.append("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(getSpitName(name)).append("\r\n");
			sb.append("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(qpEncodeing(name)).append("\r\n");
		}
		sb.append("TEL;CELL:").append(getPhone(phone)).append("\r\n");
		if (s.length >= 3) {
			sb.append("TEL;HOME:").append(getGDPhone(s[2])).append("\r\n");
		}
		sb.append("END:VCARD\r\n");
		return sb.toString();
	}

	private static String getSpitName(String name) {
		int len = name.length();
		if (len >= 3) {
			String first = name.substring(0, len - 2);
			String end2 = name.substring(len - 2, len - 1);
			String end1 = name.substring(len - 1);
			return qpEncodeing(first) + ";" + qpEncodeing(end1) + ";" + qpEncodeing(end2) + ";;";
		} else {
			String t = "";
			for (int i = 0; i < len; i++) {
				t += qpEncodeing(name.substring(i, i + 1)) + ";";
			}
			for (int i = 0; i < 4 - len; i++) {
				t += ";";
			}
			return t;
		}

	}

	/**
	 * 手机
	 * 
	 * @param s
	 * @return
	 */
	private static String getPhone(String s) {
		return s;// s.substring(0, 1) + "-" + s.substring(1, 4) + "-" +
					// s.substring(4, 7) + "-" + s.substring(7);
	}

	/**
	 * 固定电话
	 * 
	 * @param s
	 * @return
	 */
	private static String getGDPhone(String s) {
		// String r;
		// switch (s.length()) {
		// case 7:
		// r = s.substring(0, 3) + "-" + s.substring(3);
		// break;
		// case 11:
		// r = s.substring(0, 3) + "-" + s.substring(3, 6) + "-" +
		// s.substring(6);
		// break;
		// default:
		// r = "";
		// break;
		// }
		return s;
	}

	/*
	 * 解码
	 */
	public static String qpDecoding(String str) {
		if (str == null) {
			return "";
		}
		try {
			str = str.replaceAll("=\n", "");
			byte[] bytes = str.getBytes("US-ASCII");
			for (int i = 0; i < bytes.length; i++) {
				byte b = bytes[i];
				if (b != 95) {
					bytes[i] = b;
				} else {
					bytes[i] = 32;
				}
			}
			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			for (int i = 0; i < bytes.length; i++) {
				int b = bytes[i];
				if (b == '=') {
					try {
						int u = Character.digit((char) bytes[++i], 16);
						int l = Character.digit((char) bytes[++i], 16);
						if (u == -1 || l == -1) {
							continue;
						}
						buffer.write((char) ((u << 4) + l));
					} catch (ArrayIndexOutOfBoundsException e) {
						e.printStackTrace();
					}
				} else {
					buffer.write(b);
				}
			}
			return new String(buffer.toByteArray(), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/*
	 * 编码
	 */

	public static String qpEncodeing(String str) {
		char[] encode = str.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < encode.length; i++) {
			if ((encode[i] >= '!') && (encode[i] <= '~') && (encode[i] != '=') && (encode[i] != '\n')) {
				sb.append(encode[i]);
			} else if (encode[i] == '=') {
				sb.append("=3D");
			} else if (encode[i] == '\n') {
				sb.append("\n");
			} else {
				StringBuffer sbother = new StringBuffer();
				sbother.append(encode[i]);
				String ss = sbother.toString();
				byte[] buf = null;
				try {
					buf = ss.getBytes("utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				if (buf.length == 3) {
					for (int j = 0; j < 3; j++) {
						String s16 = String.valueOf(Integer.toHexString(buf[j]));
						// 抽取中文字符16进制字节的后两位,也就是=E8等号后面的两位,
						// 三个代表一个中文字符
						char c16_6;
						char c16_7;
						if (s16.charAt(6) >= 97 && s16.charAt(6) <= 122) {
							c16_6 = (char) (s16.charAt(6) - 32);
						} else {
							c16_6 = s16.charAt(6);
						}
						if (s16.charAt(7) >= 97 && s16.charAt(7) <= 122) {
							c16_7 = (char) (s16.charAt(7) - 32);
						} else {
							c16_7 = s16.charAt(7);
						}
						sb.append("=" + c16_6 + c16_7);
					}
				}
			}
		}
		return sb.toString();
	}

}
